<template>
    <div>
      <Table :columns="columns" :data="data">
        <template slot-scope="{ row, index }" slot="action">
          <Button v-if="row.manager===undefined || row.manager===''" type="primary" size="small" style="margin-right: 5px" @click="addManager(row)">添加管理员</Button>
          <Button v-else type="primary" size="small" style="margin-right: 5px" @click="updateManager(row)">修改管理员</Button>
        </template>
      </Table>
      <Modal
        v-model="modal"
        title="管理人员"
        @on-ok="disposeManage">
        <div style="text-align: center">
          <template>
            管理员：<Input v-model="manager" style="width: 300px" />
          </template>
        </div>
      </Modal>
    </div>
</template>

<script>
import { findManagerList, saveManager } from '@/api/eventlist'

export default {
  name: 'manager',
  props: {
    pickInfo: Object
  },
  data () {
    return {
      manager: '',
      row: {},
      modal: false,
      columns: [
        {
          title: '名称',
          key: 'name'
        },
        {
          title: '管理员',
          key: 'manager'
        },
        {
          title: '操作',
          slot: 'action'
        }
      ],
      data: []
    }
  },
  methods: {
    init () {
      let data = {
        id: this.pickInfo.id,
        grade: this.pickInfo.grade
      }
      findManagerList(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.data = ret.data.data
        } else {
          this.$Notice.error({
            title: '查询失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    addManager (e) {
      this.manager = ''
      this.row = {}
      this.row = e
      this.modal = true
    },
    updateManager (e) {
      this.manager = ''
      this.row = {}
      this.row = e
      this.manager = e.manager
      this.modal = true
    },
    disposeManage () {
      let data = {
        id: this.row.id,
        grade: this.row.grade,
        manager: this.manager
      }
      saveManager(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Notice.success({
            title: '保存成功',
            desc: ret.data.errmsg
          })
          this.init()
        } else {
          this.$Notice.error({
            title: '保存失败',
            desc: ret.data.errmsg
          })
        }
      })
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style scoped>

</style>
